package com.njxzc.igo.Dao;

import java.util.List;

import org.docx4j.model.datastorage.XPathEnhancerParser.main_return;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;

import com.njxzc.igo.model.Seller;
import com.njxzc.igo.model.User;
import com.njxzc.igo.model.UserType;
import com.njxzc.igo.service.UserVo;
import com.njxzc.igo.utils.ConnectionProvider;
import com.njxzc.igo.utils.HibernateUtils;

public class UserDaoImpl implements UserDao {

	public User findByName(String name) {
		Session session = null;
		Transaction tx = null;
		User user = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			List list = session.createQuery("from User as user where user.username=?").setString(0, name).list();
			user = (User) list.get(0);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return user;
	}

	public void save(User user, UserType ut) {
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			user.setUsertype(ut);
			session.saveOrUpdate(user);
			tx.commit();
			session.flush();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
	}

	public UserType findByTypeId(int typeId) {
		UserType ut = null;
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			ut = (UserType) session.get(UserType.class, typeId);
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			// HibernateUtils.closeSession();
		}
		return ut;
	}

	public static void main(String[] args) {

	}

	@Override
	public List<User> findAll() {
		List<User> list = null;
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			list = session.createQuery("select new User(userId,username,email,usertype) from User").list();
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		
		return list;
	}

	public boolean updateUserByPassword(String password, User u) {
		boolean flag = false;
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			session.update(u);
			u.setPassword(password);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return flag;
		
	}

	@Override
	public List loadUserVo(String username) {
		Session session = null;
		Transaction tx = null;
		List list = null;
		String hql = "select u.userId as userId,u.username  as username"
				+ ",u.nickname as nickname,u.email as email,u.sex as sex,u.usertype as usertype from User u where u.username=?";
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			list = session.createQuery(hql).setString(0, username).setResultTransformer((Transformers.aliasToBean(UserVo.class))).list(); 
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return list;
	}

	@Override
	public List<UserType> findAllUserTypes() {
		Session session = null;
		Transaction tx = null;
		List list = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			list = session.createQuery("select new UserType(typeId,typename) from UserType").list(); 
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		}
		return list;
	}

	@Override
	public boolean modifyUserTypes(User u, UserType ut) {
		Session session = null;
		Transaction tx = null;
		boolean flag = false;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			session.update(u);
			session.update(ut);
			u.setUsertype(ut);
			tx.commit();
			flag = true;
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return flag;	
	}

	@Override
	public User findById(String userId) {
		Session session = null;
		Transaction tx = null;
		User u = null;
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			u = (User) session.load(User.class, userId);
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		return u;
		
	}

	public void saveSaller(User user,String saler_Name) {
		Session session = null;
		Transaction tx = null;
		Seller seller = new Seller(); 
		try {
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();
			seller.setUserId(user.getUserId());
			seller.setSalerName(saler_Name);
			//seller.setSaler_Pic(saler_Pic);
			session.save(seller);
			tx.commit();
		} catch (Exception e) {
			if(tx!=null){
				tx.rollback();
			}
		}
		
	}


}
